const router = require('koa-router')()
import axios from 'axios'
import todolist from '../db/models/todolist'
import user from '../db/models/user'
import fs from 'fs'
router.prefix('/todo')
router.get('/list', async (ctx, next) => {
    var {pagesize, pagenum,username} = ctx.request.query
    let List = await todolist.find(username)
    ctx.body = {
        code: 200,
        data: List.slice((pagenum - 1) * pagesize, pagesize * pagenum ),
        total: List.length,
        msg: '获取待办事件成功'
    }
})
router.post('/addthing', async (ctx, next) => {
    var thing = ctx.request.body
    delete thing._id
    let amount = await todolist.create(thing)
    if (amount) {
        ctx.body = {
            code: 200,
            data: true,
            msg: '添加待办事件成功'
        }
    } else {
        ctx.body = {
            code: 500,
            data: null,
            msg: '操作失败'
        }
    }
    
})
router.post('/updatething', async (ctx, next) => {
    var thing = ctx.request.body
    let amount = await todolist.findByIdAndUpdate(thing._id, thing)
    if (amount) {
        ctx.body = {
            code: 200,
            data: true,
            msg: '修改待办事件成功'
        }
    } else {
        ctx.body = {
            code: 500,
            data: null,
            msg: '操作失败'
        }
    }
    
})
module.exports = router
